﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pavilion;

namespace Pavilion.Control
{
    public class HueGrid : Control
    {
        private const int HueWidth = 20;

        private CTexture2D hueTexture;
        private int gridHeight;
        private float huePositionY;
        private float increment;

        public event Action<float> HueValueChanged;

        public HueGrid(int gridHeight) : base()
        {
            this.gridHeight = gridHeight;
            this.AreaBounds = new Rectangle(0, 0, HueWidth, gridHeight);
            this.HuePositionY = gridHeight / 2;
            this.increment = 360f / gridHeight;

            RenderHueDisplay();

            this.Input.MousePress += new MouseObjectHandler(ColorGrid_MousePress);
            this.Input.MouseMoved += new MouseObjectHandler(ColorGrid_MouseMoved);
        }

        public override void Draw()
        {
            hueTexture.Draw(new Vector2(ScreenBounds.X, ScreenBounds.Y), Color.White);

            DrawManager.Instance.DrawLineOnScreen(new Vector2(ScreenBounds.Left, ScreenBounds.Top + HuePositionY - 1), new Vector2(ScreenBounds.Right, ScreenBounds.Top + HuePositionY - 1), Color.Black);
            DrawManager.Instance.DrawLineOnScreen(new Vector2(ScreenBounds.Left, ScreenBounds.Top + HuePositionY), new Vector2(ScreenBounds.Right, ScreenBounds.Top + HuePositionY), Color.White);
            DrawManager.Instance.DrawLineOnScreen(new Vector2(ScreenBounds.Left, ScreenBounds.Top + HuePositionY + 1), new Vector2(ScreenBounds.Right, ScreenBounds.Top + HuePositionY + 1), Color.Black);

            base.Draw();
            DrawManager.Instance.DrawRectangleOnScreen(ScreenBounds, OutlineColor);
        }

        private void RenderHueDisplay()
        {
            Render render = new Render();

            int yPos = 0;

            render.Begin(HueWidth, AreaBounds.Height);

            for (float h = 0f; h <= 360f; h += increment)
            {
                CColor color = new CColor();
                DrawManager.Instance.DrawLineOnScreen(new Vector2(0, yPos), new Vector2(HueWidth, yPos), color.ConvertHsbToRgb(h, 1f, 0.5f, 255));
                yPos++;
            }

            render.End();

            hueTexture = new CTexture2D(render.Texture);
        }

        /// <summary>
        /// Gets the current Hue value.
        /// </summary>
        public float CurrentHueValue
        {
            get { return huePositionY * increment; }
        }
        
        /// <summary>
        /// Gets or sets the Y position of the Hue Grid.
        /// </summary>
        private float HuePositionY
        {
            get { return huePositionY; }
            set
            {
                if (value < 0)
                    value = 0;
                else if (value > gridHeight)
                    value = gridHeight;

                if (huePositionY == value)
                    return;

                huePositionY = value;
                OnHueValueChanged();
            }
        }

        private void OnHueValueChanged()
        {
            if (HueValueChanged != null)
                HueValueChanged(CurrentHueValue);
        }

        private void ColorGrid_MousePress(object sender, MouseCursor mouseCursor)
        {
            HuePositionY = mouseCursor.Position.Y - ScreenBounds.Y;
        }

        private void ColorGrid_MouseMoved(object sender, MouseCursor mouseCursor)
        {
            if (Mouse.GetState().LeftButton == ButtonState.Released && Mouse.GetState().RightButton == ButtonState.Released)
                return;

            HuePositionY = mouseCursor.Position.Y - ScreenBounds.Y;
        }
    }
}
